package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.pub.sqlloader.SqlLoader;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.entity.powerdb.designer.EntityRelate;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.EntityRelateService;
import com.sunmnet.j2ee.service.powerdb.designer.EntityService;
import com.sunmnet.j2ee.service.powerdb.designer.FieldService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("/powerdb/designer/entityRelateService")
public class EntityRelateServiceImpl extends BaseServiceImpl<EntityRelate, Long> implements EntityRelateService {
    @Override
    @Resource(name = "/powerdb/designer/entityRelateDao")
    public void setDao(BaseDao<EntityRelate, Long> dao) {
        super.dao = dao;
    }

	@Resource(name = "/powerdb/designer/entityService")
	private EntityService entityService;

	@Resource(name = "/powerdb/designer/fieldService")
	private FieldService fieldService;
	/**
     * log4j2日志 *
     */
    private static final Logger LOG = LogManager.getLogger(EntityRelateServiceImpl.class.getName());

    private static final String CLASS_NAME = EntityRelateServiceImpl.class.getName();

    private static final String SQL_GET_ENTITY_REL_BY_ID = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_GET_ENTITY_REL_BY_ID");

	private static final String SQL_UPDATE_ENTITY_RELATE_POSITIONS = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_UPDATE_ENTITY_RELATE_POSITIONS");

    @Override
    public List<EntityRelate> getEntityRelates(Long moduleId, Long versionId) {
	    String hql = "FROM EntityRelate WHERE moduleId = " + moduleId + " AND versionId = " + versionId;
	    return dao.findByHql(hql);
    }

    @Override
    public void copyEntityRelates(List<EntityRelate> list, Map<Long, Long> copyedEntitys, Map<Long, Long> copyedFields, Long versionId, Long newModuleId) {
	    LOG.debug("copyEntityRelates: {},{},{},{},{}", list, copyedEntitys, copyedFields, versionId, newModuleId);

        EntityRelate tmp = null;

	    Long startObjId, endObjId, startFieldId, endFieldId;
	    Long newStartObjId, newEndObjId, newStartFieldId, newEndFieldId;

	    for (EntityRelate en : list) {
	        startObjId = en.getStartObjectId();
		    newStartObjId = copyedEntitys.get(startObjId);

	        endObjId = en.getEndObjectId();
		    newEndObjId = copyedEntitys.get(endObjId);

	        startFieldId = en.getStartFieldId();
		    newStartFieldId = copyedFields.get(startFieldId);

	        endFieldId = en.getEndFieldId();
		    newEndFieldId = copyedFields.get(endFieldId);

		    tmp = clone(en, versionId, newModuleId, newStartObjId, newEndObjId, newStartFieldId, newEndFieldId);
		    dao.save(tmp);
	    }
    }

	private EntityRelate clone(EntityRelate en, Long versionId, Long newModuleId, Long newStartObjId, Long newEndObjId, Long newStartFieldId, Long newEndFieldId) {
		EntityRelate entityRelate = new EntityRelate();
        entityRelate.setVersionId(versionId);
		entityRelate.setModuleId(newModuleId);
		entityRelate.setName(en.getName());
        entityRelate.setNameCn(en.getNameCn());
		entityRelate.setStartObjectId(newStartObjId);
		entityRelate.setEndObjectId(newEndObjId);
		entityRelate.setPoints(en.getPoints());
		entityRelate.setEndFieldId(newEndFieldId);
		entityRelate.setCreateTime(en.getCreateTime());
        entityRelate.setMultiplicityJson(en.getMultiplicityJson());
		entityRelate.setStartFieldId(newStartFieldId);
		entityRelate.setModifyTime(en.getModifyTime());
        entityRelate.setModifyAccountId(en.getModifyAccountId());
        entityRelate.setCreateAccountId(en.getCreateAccountId());
        entityRelate.setRemark(en.getRemark());
        return entityRelate;
    }


    @Override
    public List getEntitysByEntityRelatId(Long entityRelateId) {
        Map<String, Object> params = new HashMap<>();
	    List<Map<String, Object>> list = null;
	    params.put("id", entityRelateId);
	    try {
//            LOG.debug(SQL_GET_ENTITY_REL_BY_ID);
            list = dao.findBySql(SQL_GET_ENTITY_REL_BY_ID, params);
        } catch (Exception e) {
            LOG.error("查询失败", e);
        }
			/*long startFieldId = Long.parseLong(list.get(0).get("startFieldId").toString());
		    long endFieldId =  Long.parseLong(list.get(0).get("endFieldId").toString());

			if(startFieldId != null){
	            Field startField = fieldDao.get(startFieldId);
	            list.get(0).put("startFieldName",startField.getName());
	        }
	        if(endFieldId != null){
	            Field endField = fieldDao.get(endFieldId);
	            list.get(0).put("endFieldName",endField.getName());
	        }*/

        return list;
    }

	/**
	 * @param id
	 * @param points
	 * @param multiplicityJson
	 * @return
	 */
	@Override
	public int updatePoints(Long id, String points, String multiplicityJson) {
		int result;

		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		params.put("points", points);
		params.put("multiplicityJson", multiplicityJson
				.replace("&quot;", PDMServiceImpl.SYNBOL)
				.replace("\"", PDMServiceImpl.SYNBOL));

		return dao.executeSql(SQL_UPDATE_ENTITY_RELATE_POSITIONS, params);
	}
}
